package ac.svn.accesscontrol.ui.panel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;

import ac.svn.accesscontrol.store.IResources;
import ac.svn.accesscontrol.ui.WhiteLabel;
import ac.svn.accesscontrol.ui.model.ThreadedListModel;
import ac.svn.accesscontrol.ui.renderer.SimpleIconRenderer;
import ac.svn.accesscontrol.utils.UIUtility;
import ac.svn.accesscontrol.utils.Utility;

public class UserGroupEditorPanel extends JPanel {

    public interface ExportedComponents {
	// Variable belonging to user group membership panel.
	public static final String C_UGM_PANEL = "C_UGM_PANEL";
	public static final String C_UGM_AVL_GRP_LIST_VIEW = "C_UGM_AVL_GRP_LIST_VIEW";
	public static final String C_UGM_AVL_GRP_LIST_MODEL = "C_UGM_AVL_GRP_LIST_MODEL";
	public static final String C_UGM_ASND_PANEL = "C_UGM_ASND_PANEL";
    }

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private WhiteLabel lblGroupCount, lblUser;
    private JButton btnAdd;
    private JList listAvailableGroups;
    private ThreadedListModel listModel;
    private JPanel pnlGroups;
    private JScrollPane scrollPaneAvlGrps;

    public UserGroupEditorPanel() {
	super(new BorderLayout(0, 0), true);
	initCompnents();
	//registerComponents();
    }

    private void initCompnents() 
    {
	Color WHITE = Color.WHITE;
	Border border = UIUtility.setAllShadowedBorder(null);
	
	JPanel panelInfo = new JPanel();
	panelInfo.setBackground(WHITE);
	panelInfo.setBorder(border);
	panelInfo.setPreferredSize(new Dimension(10, 40));
	panelInfo.setLayout(new BorderLayout(10, 0));
	
	add(panelInfo, BorderLayout.NORTH);
	

	lblUser = new WhiteLabel("User Name",
		Utility.getImageIcon(IResources.LOC_ICN_USER),
		SwingConstants.LEFT);
	panelInfo.add(lblUser, BorderLayout.CENTER);

	lblGroupCount = new WhiteLabel("Group(s): 0");
	lblGroupCount.enableShadow(true);
	panelInfo.add(lblGroupCount, BorderLayout.EAST);

	JSplitPane splitPane = new JSplitPane();
	splitPane.setBorder(border);
	splitPane.setBackground(WHITE);
	add(splitPane, BorderLayout.CENTER);

	JScrollPane scpnUserGroup = new JScrollPane();
	scpnUserGroup.setBackground(WHITE);
	splitPane.setRightComponent(scpnUserGroup);

	pnlGroups = new JPanel(true);
	pnlGroups.setLayout(new GridLayout(50, 1, 0, 5));
	pnlGroups.setBackground(WHITE);
	pnlGroups.setBorder(border);

	scpnUserGroup.setViewportView(pnlGroups);

	JLabel lblFollowingGroupsAre = new WhiteLabel(
		"<HTML><i>User is member of below listed groups</i></HTML>");
	lblFollowingGroupsAre.setPreferredSize(new Dimension(221, 40));
	lblFollowingGroupsAre.setBorder(border);
	
	scpnUserGroup.setColumnHeaderView(lblFollowingGroupsAre);

	scrollPaneAvlGrps = new JScrollPane();
	scrollPaneAvlGrps.setBackground(WHITE);
	scrollPaneAvlGrps.setPreferredSize(new Dimension(20, 2));
	
	splitPane.setLeftComponent(scrollPaneAvlGrps);

	listAvailableGroups = new JList();
	listModel = new ThreadedListModel();
	listAvailableGroups.setModel(listModel);
	listAvailableGroups.setCellRenderer(new SimpleIconRenderer(
		IResources.LOC_ICN_GROUP_SMALL));
	scrollPaneAvlGrps.setViewportView(listAvailableGroups);

	JPanel panel = new JPanel();
	panel.setBorder(border);
	panel.setBackground(WHITE);
	panel.setPreferredSize(new Dimension(10, 40));
	panel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
	
	scrollPaneAvlGrps.setColumnHeaderView(panel);

	WhiteLabel lblAvailableGroups = new WhiteLabel("Available Groups");
	panel.add(lblAvailableGroups);

	btnAdd = new JButton("+");
	panel.add(btnAdd);
	splitPane.setDividerLocation(250);
    }

    public JLabel getLblGroupCount() {
	return lblGroupCount;
    }

    public JLabel getLblUser() {
	return lblUser;
    }

    public JButton getBtnAdd() {
	return btnAdd;
    }

    public JList getListAvailableGroups() {
	return listAvailableGroups;
    }

    public ThreadedListModel getListModel() {
	return listModel;
    }

    public void resetData() {
	pnlGroups.removeAll();
	lblGroupCount.setText("Group(s): 0 ");
	lblUser.setText("User Name");
	listModel.clear();
	SwingUtilities.invokeLater(new Runnable() {
	    @Override
	    public void run() {
		SwingUtilities.updateComponentTreeUI(listAvailableGroups);
	    }
	});
    }

    public JPanel getPnlGroups() {
	return pnlGroups;
    }

    /**
     * @return the scrollPaneAvlGrps
     */
    public JScrollPane getScrollPaneAvlGrps() {
        return scrollPaneAvlGrps;
    }

    /**
     * @param scrollPaneAvlGrps the scrollPaneAvlGrps to set
     */
    public void setScrollPaneAvlGrps(JScrollPane scrollPaneAvlGrps) {
        this.scrollPaneAvlGrps = scrollPaneAvlGrps;
    }
}
